<?php

namespace App\Http\Controllers\Web;

use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Services\SmsService;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;


class AccountsController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:web');
    }
    /**
     * 编辑个人信息
     * @param Request $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function user(Request $request)
    {

        $users =  Auth::user();
        if($request->getMethod()=="POST"){
         User::where('id',$users->id)->update([

                               'realname' => $request->input("realname"),
                               'cidnum' => $request->input("cidnum"),
                               'qq' => $request->input("qq"),
                               'province' => $request->get("province"),
                               'city' => $request->get("city"),
                               'address' => $request->input("address"),
                             ]);
         return redirect($request->getPathInfo());
        }

          return view('web.accounts.user',['realname'=>$users->realname,'cidnum'=>$users->cidnum,'qq'=>$users->qq,
                                           'address'=>$users->address,'province'=>$users->province,'city'=>$users->city]);
    }
    public function send($email,$id){
       $key=array('sd899sd8j3k7sd21poi2433',$email,$id,time());
       $key=implode(',',$key);
       $key=base64_encode($key);
       Mail::to($email)->send(new SendMall($key));
   }

    /**
     * 验证邮箱
     * @param Request $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function email(Request $request)
    {
       //查询该用户是否已经认证
        $id = Auth::id();
        if($request->get('newren')==2){
            $data['emailkey']=1;
            $re =  User::where('id',$id)->update($data);
            return redirect('/accounts/email');
        }else{
            $res = User::where('id',$id)->first();
        }
        $ms=1;
        if($request->isMethod('post')){
            $rules = [
                  'captcha' => 'required|captcha',
                  'email'=>'required|email',
              ];
          $messages = [
                        'captcha.required'    => '验证码不能为空',
                        'captcha.captcha'    => '验证码有错误',
                        'email.required'=>'邮箱不能为空',
                        'email.email'=>'邮箱格式不正确'
                      ];
          $validator= Validator::make($request->all(),$rules,$messages);
          if ($validator->fails()) {
             return redirect('/accounts/email')->withErrors($validator)->withInput();
           }else{
               $ms=2;
              $this->send($request->input('email'),$id);
          }
        }

        return view('web.accounts.email',compact('res','ms'));

    }
    /**
     * [sms 发送短信]
     * @return [type] [发送短信]
     */
    public function sendMsg($phone)
    {
        $Verify = mt_rand(100000, 999999);

        $data = array((string)$Verify, '5');
        session(['phonecode' => $data]);
        $rest = SmsService::sendSMS($phone, $data);
        if ($rest == null) {
            return false;
        }
        if ($rest->statusCode != 0) {
            return false;
            exit("发送失败! 状态：" . $rest->statusCode);
        } else {
            return true;
        }
    }

    /**
     * 验证手机号
     * @param Request $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function phone(Request $request)
    {
        //查询该用户是否已经认证
        $id = Auth::id();
        $res = User::where('id',$id)->first();
        $phone=$res->telphone;
        $phonekey=$res->phonekey;
        if($request->isMethod('post')){
          $rules=[
                'captcha' => 'captcha',
            ];
            $messages=[
                'captcha.captcha'=>'验证码不正确'
            ];
            $validator= Validator::make($request->all(),$rules,$messages);
            if ($validator->fails()) {
                return redirect('/accounts/phone')->withErrors($validator)->withInput();
            }else{
                if($this->sendMsg($request->input('telphone'))){
                    $phone=$request->input('telphone');
                }else{
                    exit('短信发送失败');
                };
            }
        }
        return view('web.accounts.phone',compact('phonekey','phone'));
    }
    public function chkcode(Request $request){
        $id = Auth::id();
        if($request->isMethod('post')){
            $rules=[
                'phonecode' => 'required',
            ];
            $messages=[
                'phonecode'=>'短信验证码不能为空'
            ];
            $validator= Validator::make($request->all(),$rules,$messages);
            if ($validator->fails()) {
                return redirect('/accounts/phone')->withErrors($validator)->withInput();
            }else{
                $phonecode=session('phonecode');
                if($request->input('phonecode')==$phonecode[0]){
                    $dataup['telphone']=$request->input('cphone');
                    $dataup['phonekey']=2;
                    $dataup['phone_time']=time();
                    $phonekey=$dataup['phonekey'];
                    $phone=$dataup['telphone'];
                    $re = User::where('id',$id)->update($dataup);
                    if(!$re){
                        return back();
                    }
            }else{
                    return back()->with('msg','验证码错误');
                }
        }
      }
        return view('web.accounts.phone',compact('phonekey','phone'));
    }

    /**
     * @param Request $request
     */
    public function monitor(Request $request)
    {
        return view('web.accounts.monitor');
    }

    public function edit($id)
    {
        //
    }

    public function update(Request $request, $id)
    {
        //
    }


    public function destroy($id)
    {
        //
    }
}
